// PROGRAMA : IDCPB014(PROGRAMA BATCH)  22/03/2002
// BIBLIOTECA : SISP
// OBJETIVO : GERAR ARQUIVO PARA AMERICAN BANK NOTE COMPANY - ABN
// : CRIMINAL, excluindo os registros ja gravados
// : USER = RENACHMA
// : PASSWORD = ABN738
// : IP = 100.249.158.131
// OBS : O PROGRAMA IDCPB008 DEVE SER RODADO EM SEGUIDA

package br.com.vti.sisp_sicrim.antigo.controller.programa;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.CharBuffer;

import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.vti.sisp_sicrim.antigo.model.SicCadastro;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Idcpb014 OBJETIVO : GERAR ARQUIVO PARA AMERICAN BANK NOTE COMPANY - ABN *
 * 
 * @author Incode - Data: mai/2014
 * 
 */

@Resource
@Path("/antigo/idcpb014")
public class Idcpb014 {
	// Views utilizadas
	SicCadastro sicCadastroView = new SicCadastro();
	// Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();
	FileWriter arquivo2;
	FileReader arquivo1;

	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;
	private CharBuffer rgeral;
	private String wrg;
	private String wpoldir;
	private String winddir;
	private String wmeddir;
	private String wanudir;
	private String wmindir;
	private String wpolesq;
	private String windesq;
	private String wmedesq;
	private String wanuesq;
	private String wminesq;
	private int wstatusReg;
	private String wtipoReg;
	private int wnumpos;
	private long wnupro;
	private long wregistroEstadualGeral;
	private long wregistroFederalCi;
	private String wigUf;
	private String wnomeCompleto;
	private String wnomePai;
	private String wnomeMae;
	private String wnomeAnterior;
	private int wdataReg;
	private String[] wapelidoAlcunhas;
	private int wdataNasc;
	private int wcompleicao;
	private String wufRegEstadualGeral;
	private String wmunicipioRegGeral;
	private int wcodMunicRegGeral;
	private String wdocumApresentado;
	private long wnumeDocumApresent;
	private String worgaoExpUf;
	private int wnacionalidadeEstrangeiro;
	private int wtipoProprietarioArma;
	private String wdataUltimaMovimentUf;
	private String wpossuiInquerito;
	private String wpossuiArma;
	private String wpossuiProcesso;
	private String wfalecido;
	private String wpossuiMandatoPAberto;
	private String worgaoExp;
	private String wufExp;
	private String wnomeDelegacia;
	private int wsexo;
	private int wcutis;
	private int wcorCabelos;
	private int wtipoCabelos;
	private int wcorOlhos;
	private int wsobrancelhas;
	private int wbarba;
	private int wbigode;
	private int waltura;
	private String wmarcaPartCicatTatuag;
	private String wprofissao;
	private int westatoCivil;
	private int wnacionalidade;
	private String wnaturalidade;
	private int wcodMunicipioNascimento;
	private String wufNaturalidade;
	private int wgrauInstrucao;
	private String wavRua;
	private int wnumero;
	private String wbairro;
	private int wmunicipio;
	private String westado;
	private int wcep;
	private String wcomplemento;
	private String wmotivoPrisaoLei;
	private long wnumeroAutos;
	private int wanoAutos;
	private int wdataAtuacao;
	private String whoraPrisao;
	private String wnomeVitima;
	private String wlocal;
	private String whaInqueritoPolic;
	private String wquerFolhaAnteced;
	private int wmatfuncAtu;
	private int wdatdocAtu;
	private int whoraAtu;
	private String[] wobs;
	private String wregistroOutrosEstados;
	private String wregCrim; // END

	private int i;
	private int wc = 0;

	public Idcpb014(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public void executar() {
		try {
			main();
		} catch (Exception e) {
			ExceptionProgram excp = new ExceptionProgram();
			excp.trataErro(e, sessao, this.getClass().getSimpleName());
		}
	}

	private void main() throws NumberFormatException, IOException {
		File file = new File(UtilBean.pathFiles() + "arquivo1");
		// creates the file
		if (!file.exists()) {
			file.createNewFile();
		}
		arquivo1 = new FileReader(UtilBean.pathFiles() + "arquivo1");
		arquivo2 = new FileWriter(UtilBean.pathFiles() + "arquivo2");

		while (sicCadastroView.leitura()) {
			// READADA
			while (arquivo1.read(rgeral) > 0) {
				wrg = String.valueOf(rgeral);
				if (sicCadastroView.getRegEstadualGeral() == Integer.parseInt(UtilBean.subStr(wrg, 0, 7))) {
					break;
				}

				wregCrim = "";
				i = 0;
				for (i = 1; i <= 4; i++) {
					if (sicCadastroView.getApelidoAlcunhas(i).trim().equals("")) {
						break;
					} else {
						wapelidoAlcunhas[i] = sicCadastroView.getApelidoAlcunhas(i);
					}

				}
				i = 0;
				for (i = 1; i <= 17; i++) {
					if (sicCadastroView.getObservacao(i).trim().equals("")) {
						break;
					} else {
						wobs[i] = sicCadastroView.getObservacao(i);
					}

				}
				wpoldir = sicCadastroView.getPolegarDireito();
				winddir = sicCadastroView.getIndicadorDireito();
				wmeddir = sicCadastroView.getMedioDireito();
				wanudir = sicCadastroView.getAnularDireito();
				wmindir = sicCadastroView.getMinimoDireito();
				wpolesq = sicCadastroView.getPolegarEsquerdo();
				windesq = sicCadastroView.getIndicadorEsquerdo();
				wmedesq = sicCadastroView.getMedioEsquerdo();
				wanuesq = sicCadastroView.getAnularEsquerdo();
				wminesq = sicCadastroView.getMinimoEsquerdo();
				wstatusReg = sicCadastroView.getStatusReg();
				wtipoReg = sicCadastroView.getTipoReg();
				wnumpos = sicCadastroView.getNumPos();
				wnupro = sicCadastroView.getNupro();
				wregistroEstadualGeral = sicCadastroView.getRegEstadualGeral();
				wregistroFederalCi = sicCadastroView.getRegFederalCi();
				wigUf = sicCadastroView.getIgUf();
				wnomeCompleto = sicCadastroView.getNomeCompleto();
				wnomePai = sicCadastroView.getNomePai();
				wnomeMae = sicCadastroView.getNomeMae();
				wnomeAnterior = sicCadastroView.getNomeAnterior();
				wdataReg = sicCadastroView.getDataReg();
				wdataNasc = sicCadastroView.getDataNasc();
				wcompleicao = sicCadastroView.getCompleicao();
				wufRegEstadualGeral = sicCadastroView.getUfRegEstadualGeral();
				wmunicipioRegGeral = sicCadastroView.getMunicipioRegGeral();
				wcodMunicRegGeral = sicCadastroView.getCodigoMunicipioRegGeral();
				wdocumApresentado = sicCadastroView.getDocumentoApresent();
				// WNUME-DOCUM-APRESENT := NUME-DOCUM-APRESENT

				worgaoExpUf = sicCadastroView.getOrgaoExpUfUfUf();
				wnacionalidadeEstrangeiro = sicCadastroView.getNacionEstrangeiro();
				wtipoProprietarioArma = sicCadastroView.getTipoProprietarioArma();
				wdataUltimaMovimentUf = sicCadastroView.getDataUltimaMovimentUf();
				wpossuiInquerito = sicCadastroView.getPossuiInquerito();
				wpossuiArma = sicCadastroView.getPossuiArma();
				wpossuiProcesso = sicCadastroView.getPossuiProcesso();
				wfalecido = sicCadastroView.getFalecido();
				wpossuiMandatoPAberto = sicCadastroView.getPossuiMandatoAberto();
				worgaoExp = sicCadastroView.getOrgaoExp();
				wufExp = sicCadastroView.getUfExp();
				wnomeDelegacia = sicCadastroView.getNomeDelegacia();
				wsexo = sicCadastroView.getSexo();
				wcutis = sicCadastroView.getCodigoCutis();
				wcorCabelos = sicCadastroView.getCodigoCorCabelo();
				wtipoCabelos = sicCadastroView.getCodigoTipoCabelo();
				wcorOlhos = sicCadastroView.getCodigoCorOlhos();
				wsobrancelhas = sicCadastroView.getCodigoSobrancelhas();
				wbarba = sicCadastroView.getCodigoBarba();
				wbigode = sicCadastroView.getCodigoBigode();
				waltura = sicCadastroView.getAltura();
				wmarcaPartCicatTatuag = sicCadastroView.getMarcaPartCicatTatuag();
				wprofissao = sicCadastroView.getProfissao();
				westatoCivil = sicCadastroView.getEstadoCivil();
				wnacionalidade = sicCadastroView.getNacionalidade();
				wnaturalidade = sicCadastroView.getNaturalidade();
				wcodMunicipioNascimento = sicCadastroView.getCodigoMunicipioNasc();
				wufNaturalidade = sicCadastroView.getUfNaturalidade();
				wgrauInstrucao = sicCadastroView.getGrauInstrucao();
				wavRua = sicCadastroView.getAvRua();
				wnumero = sicCadastroView.getNumero();
				wbairro = sicCadastroView.getBairro();
				wmunicipio = sicCadastroView.getMunicipio();
				westado = sicCadastroView.getEstado();
				wcep = sicCadastroView.getCep();
				wcomplemento = sicCadastroView.getComplemento();
				wmotivoPrisaoLei = sicCadastroView.getMotivoPrisaoLei();
				// WNUMERO-AUTOS := NUMERO-AUTOS
				// WANO-AUTOS := ANO-AUTOS

				wdataAtuacao = sicCadastroView.getDataAtuacao();
				whoraPrisao = sicCadastroView.getHoraPrisao();
				wnomeVitima = sicCadastroView.getNomeVitima();
				wlocal = sicCadastroView.getLocal();
				whaInqueritoPolic = sicCadastroView.getInqueritoPolicial();
				wquerFolhaAnteced = sicCadastroView.getQuerFolhaAnteced();
				wmatfuncAtu = sicCadastroView.getMatriculaFuncAtu();
				wdatdocAtu = sicCadastroView.getDataDocAtu();
				whoraAtu = sicCadastroView.getHoraAtu();
				wregistroOutrosEstados = sicCadastroView.getRegOutrosEstados();
				wregCrim = UtilBean.moveSubstring(wregCrim, 0, 7, "A", wpoldir);
				wregCrim = UtilBean.moveSubstring(wregCrim, 7, 7, "A", winddir);
				wregCrim = UtilBean.moveSubstring(wregCrim, 14, 7, "A", wmeddir);
				wregCrim = UtilBean.moveSubstring(wregCrim, 21, 7, "A", wanudir);
				wregCrim = UtilBean.moveSubstring(wregCrim, 28, 7, "A", wmindir);
				wregCrim = UtilBean.moveSubstring(wregCrim, 35, 7, "A", wpolesq);
				wregCrim = UtilBean.moveSubstring(wregCrim, 42, 7, "A", windesq);
				wregCrim = UtilBean.moveSubstring(wregCrim, 49, 7, "A", wmedesq);
				wregCrim = UtilBean.moveSubstring(wregCrim, 56, 7, "A", wanuesq);
				wregCrim = UtilBean.moveSubstring(wregCrim, 63, 7, "A", wminesq);
				wregCrim = UtilBean.moveSubstring(wregCrim, 70, 1, "N", wstatusReg);
				wregCrim = UtilBean.moveSubstring(wregCrim, 71, 2, "A", wtipoReg);
				wregCrim = UtilBean.moveSubstring(wregCrim, 73, 3, "N", wnumpos);
				wregCrim = UtilBean.moveSubstring(wregCrim, 76, 9, "N", wnupro);
				wregCrim = UtilBean.moveSubstring(wregCrim, 85, 11, "N", wregistroEstadualGeral);
				wregCrim = UtilBean.moveSubstring(wregCrim, 96, 11, "N", wregistroFederalCi);
				wregCrim = UtilBean.moveSubstring(wregCrim, 107, 20, "A", wigUf);
				wregCrim = UtilBean.moveSubstring(wregCrim, 127, 60, "A", wnomeCompleto);
				wregCrim = UtilBean.moveSubstring(wregCrim, 187, 60, "A", wnomePai);
				wregCrim = UtilBean.moveSubstring(wregCrim, 247, 60, "A", wnomeMae);
				wregCrim = UtilBean.moveSubstring(wregCrim, 307, 60, "A", wnomeAnterior);
				wregCrim = UtilBean.moveSubstring(wregCrim, 367, 8, "N", wdataReg);
				wregCrim = UtilBean.moveSubstring(wregCrim, 375, 30, "A", wapelidoAlcunhas);
				wregCrim = UtilBean.moveSubstring(wregCrim, 405, 8, "N", wdataNasc);
				wregCrim = UtilBean.moveSubstring(wregCrim, 413, 2, "N", wcompleicao);
				wregCrim = UtilBean.moveSubstring(wregCrim, 415, 2, "A", wufRegEstadualGeral);
				wregCrim = UtilBean.moveSubstring(wregCrim, 417, 30, "A", wmunicipioRegGeral);
				wregCrim = UtilBean.moveSubstring(wregCrim, 447, 4, "N", wcodMunicRegGeral);
				wregCrim = UtilBean.moveSubstring(wregCrim, 451, 30, "A", wdocumApresentado);
				wregCrim = UtilBean.moveSubstring(wregCrim, 481, 11, "N", wnumeDocumApresent);
				wregCrim = UtilBean.moveSubstring(wregCrim, 492, 15, "A", worgaoExpUf);
				wregCrim = UtilBean.moveSubstring(wregCrim, 507, 4, "N", wnacionalidadeEstrangeiro);
				wregCrim = UtilBean.moveSubstring(wregCrim, 511, 2, "N", wtipoProprietarioArma);
				wregCrim = UtilBean.moveSubstring(wregCrim, 513, 8, "A", wdataUltimaMovimentUf);
				wregCrim = UtilBean.moveSubstring(wregCrim, 521, 1, "A", wpossuiInquerito);
				wregCrim = UtilBean.moveSubstring(wregCrim, 522, 1, "A", wpossuiArma);
				wregCrim = UtilBean.moveSubstring(wregCrim, 523, 1, "A", wpossuiProcesso);
				wregCrim = UtilBean.moveSubstring(wregCrim, 524, 1, "A", wfalecido);
				wregCrim = UtilBean.moveSubstring(wregCrim, 525, 1, "A", wpossuiMandatoPAberto);
				wregCrim = UtilBean.moveSubstring(wregCrim, 526, 15, "A", worgaoExp);
				wregCrim = UtilBean.moveSubstring(wregCrim, 541, 2, "A", wufExp);
				wregCrim = UtilBean.moveSubstring(wregCrim, 543, 30, "A", wnomeDelegacia);
				wregCrim = UtilBean.moveSubstring(wregCrim, 573, 1, "N", wsexo);
				wregCrim = UtilBean.moveSubstring(wregCrim, 574, 1, "N", wcutis);
				wregCrim = UtilBean.moveSubstring(wregCrim, 575, 1, "N", wcorCabelos);
				wregCrim = UtilBean.moveSubstring(wregCrim, 576, 1, "N", wtipoCabelos);
				wregCrim = UtilBean.moveSubstring(wregCrim, 577, 1, "N", wcorOlhos);
				wregCrim = UtilBean.moveSubstring(wregCrim, 578, 1, "N", wsobrancelhas);
				wregCrim = UtilBean.moveSubstring(wregCrim, 579, 1, "N", wbarba);
				wregCrim = UtilBean.moveSubstring(wregCrim, 580, 1, "N", wbigode);
				wregCrim = UtilBean.moveSubstring(wregCrim, 581, 1, "N", waltura);
				wregCrim = UtilBean.moveSubstring(wregCrim, 582, 60, "A", wmarcaPartCicatTatuag);
				wregCrim = UtilBean.moveSubstring(wregCrim, 642, 30, "A", wprofissao);
				wregCrim = UtilBean.moveSubstring(wregCrim, 672, 1, "N", westatoCivil);
				wregCrim = UtilBean.moveSubstring(wregCrim, 673, 2, "N", wnacionalidade);
				wregCrim = UtilBean.moveSubstring(wregCrim, 675, 35, "A", wnaturalidade);
				wregCrim = UtilBean.moveSubstring(wregCrim, 710, 4, "N", wcodMunicipioNascimento);
				wregCrim = UtilBean.moveSubstring(wregCrim, 714, 2, "A", wufNaturalidade);
				wregCrim = UtilBean.moveSubstring(wregCrim, 716, 1, "N", wgrauInstrucao);
				wregCrim = UtilBean.moveSubstring(wregCrim, 717, 30, "A", wavRua);
				wregCrim = UtilBean.moveSubstring(wregCrim, 747, 8, "N", wnumero);
				wregCrim = UtilBean.moveSubstring(wregCrim, 755, 20, "A", wbairro);
				wregCrim = UtilBean.moveSubstring(wregCrim, 775, 4, "N", wmunicipio);
				wregCrim = UtilBean.moveSubstring(wregCrim, 779, 2, "A", westado);
				wregCrim = UtilBean.moveSubstring(wregCrim, 781, 8, "N", wcep);
				wregCrim = UtilBean.moveSubstring(wregCrim, 789, 20, "A", wcomplemento);
				wregCrim = UtilBean.moveSubstring(wregCrim, 809, 60, "A", wmotivoPrisaoLei);
				wregCrim = UtilBean.moveSubstring(wregCrim, 869, 11, "N", wnumeroAutos);
				wregCrim = UtilBean.moveSubstring(wregCrim, 880, 4, "N", wanoAutos);
				wregCrim = UtilBean.moveSubstring(wregCrim, 884, 8, "N", wdataAtuacao);
				wregCrim = UtilBean.moveSubstring(wregCrim, 892, 8, "A", whoraPrisao);
				wregCrim = UtilBean.moveSubstring(wregCrim, 900, 60, "A", wnomeVitima);
				wregCrim = UtilBean.moveSubstring(wregCrim, 960, 30, "A", wlocal);
				wregCrim = UtilBean.moveSubstring(wregCrim, 990, 1, "A", whaInqueritoPolic);
				wregCrim = UtilBean.moveSubstring(wregCrim, 991, 1, "A", wquerFolhaAnteced);
				wregCrim = UtilBean.moveSubstring(wregCrim, 992, 7, "N", wmatfuncAtu);
				wregCrim = UtilBean.moveSubstring(wregCrim, 999, 8, "N", wdatdocAtu);
				wregCrim = UtilBean.moveSubstring(wregCrim, 1007, 8, "N", whoraAtu);
				wregCrim = UtilBean.moveSubstring(wregCrim, 1015, 80, "A", wobs);
				wregCrim = UtilBean.moveSubstring(wregCrim, 1095, 20, "A", wregistroOutrosEstados);
				arquivo2.write(wregCrim);
				wc += 1;
			}
		}
		sicCadastroView.fecha();
		UtilBean.mensagemTexto("registros gravados = " + wc);
		arquivo1.close();
		arquivo2.close();
	}

}
